<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/ServiceCom.php");

$servicecom = new ServiceCom();

$cont = $_REQUEST['cont'];
switch ($cont){
    case 1://增
        $name = $_REQUEST['name'];
        $fee = $_REQUEST['fee'];
        $countfee = $_REQUEST['countfee'];
        $isncard = $_REQUEST['isncard'];
        if(empty($name)||empty($fee)||empty($countfee)){
            echo json_encode(array("code" => 100, "msg" => "empty name or fee"));
            break;
        }
        $fee = (int)($fee * 100);
        $countfee = (int)($countfee * 100);
        $res = $servicecom->addService($name, $fee,$countfee,$isncard);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "add error"));
        }
        break;
    case 2://查
        $serviceid = $_REQUEST['serviceid'];
        if(empty($serviceid)){
            echo json_encode(array("code" => 100, "msg" => "empty serviceid"));
            break;
        }
        $service = $servicecom->getServiceById($serviceid);
        if(!$service){
            echo json_encode(array("code" => 400, "msg" => "search service failed"));
            break;
        }
        $result = array("code"=>0,"msg"=>"success","data"=>array());
        $result["data"]["name"] = $service['name'];
        $result["data"]["fee"] = sprintf("%.2f",$service['fee']/100);
        $result["data"]["countfee"] = sprintf("%.2f",$service['countfee']/100);
        echo json_encode($result);
        break;
    case 3://改
        $serviceid = $_REQUEST['serviceid'];
        $fee = $_REQUEST['fee'];
        $name = $_REQUEST['name'];
        $countfee = $_REQUEST['countfee'];
        if(empty($serviceid)||empty($fee)||empty($name)||empty($countfee)){
            echo json_encode(array("code" => 100, "msg" => "empty info"));
            break;
        }
        $fee = (int)($fee * 100);
        $countfee = (int)($countfee * 100);
        $res = $servicecom->updateService($serviceid, $name, $fee,$countfee);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success"));
        }else{
            echo json_encode(array("code" => 400, "msg" => "update error"));
        }
        break;
    case 4://删
        $serviceid = $_REQUEST['serviceid'];
        if(empty($serviceid)){
            echo json_encode(array("code" => 100, "msg" => "empty serviceid"));
            break;
        }
        $res = $servicecom->deleteService($serviceid);
        if($res){
            echo json_encode(array("code" => 0, "msg" => "success","data"=>$serviceid));
        }else{
            echo json_encode(array("code" => 400, "msg" => "delete error"));
        }
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined error"));
        break;
}
exit();
